//
//  SearchViewController.m
//  Funny
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 King. All rights reserved.
//

#import "SearchViewController.h"

@interface SearchViewController ()<UISearchBarDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UISearchBar *search;
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) NSArray *cityArr;

@end

@implementation SearchViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.titleView = self.search;
    
    [self.view addSubview:self.collection];
    UILabel *hot = [[UILabel alloc]initWithFrame:CGRectMake(20, 30, 90, 30)];
    hot.text = @"热门搜索:";
    hot.textColor = [UIColor colorWithRed:0 green:220.0 / 256.0 blue:220.0 / 256.0 alpha:1.0];
    [self.view addSubview:hot];

    self.cityArr = @[@"首尔",@"曼谷",@"巴黎",@"巴厘岛",@"东京",@"大阪",@"柏林",@"普吉岛",@"垦丁",@"釜山",@"清迈",@"高雄",@"罗马",@"马德里",@"布拉格",@"夏威夷",@"多伦多",@"塞班",@"开罗",@"巴塞罗那"];
}

- (UICollectionView *)collection
{
    if (!_collection) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.minimumInteritemSpacing = 10;
        flow.minimumLineSpacing = 10;
        flow.itemSize = CGSizeMake(90 * W, 30);
        self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 60, ScreenWidth, ScreenHeight) collectionViewLayout:flow];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.backgroundColor = [UIColor whiteColor];
        _collection.contentInset = UIEdgeInsetsMake(10, 20 * W, 0, 20 * W);
        [_collection registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        _collection.showsVerticalScrollIndicator = NO;
    }
    return _collection;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _cityArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.name = _cityArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchResultViewController *recultVC = [SearchResultViewController shareViewController];
    recultVC.cityName = _cityArr[indexPath.row];
    [self.navigationController pushViewController:recultVC animated:NO];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.search resignFirstResponder];
}

- (UISearchBar *)search
{
    if (!_search) {
        self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 30, ScreenWidth, 30)];
        _search.tintColor = [UIColor whiteColor];
        _search.placeholder = @"搜索目的地/景点/酒店";
        _search.barTintColor = [UIColor whiteColor];
        _search.searchBarStyle = UISearchBarStyleMinimal;
        _search.delegate = self;
    }
    return _search;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    self.search.showsCancelButton = YES;
    for (UIView *view in [[_search.subviews lastObject] subviews]) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *cancelBtn = (UIButton *)view;
            [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        }
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.search resignFirstResponder];
    self.search.showsCancelButton = NO;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.search resignFirstResponder];
    self.search.showsCancelButton = NO;
    SearchResultViewController *recultVC = [SearchResultViewController shareViewController];
    recultVC.cityName = searchBar.text;
    [self.navigationController pushViewController:recultVC animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
